<template>
  <div class="specificationConfig-add-update-component">
    <el-dialog
      v-if="modelValue.visible"
      v-model="modelValue.visible"
      :title="
        !modelValue.form.id
          ? `新增${modelValue.title}`
          : `修改${modelValue.title}`
      "
      width="550px"
    >
      <div style="padding-right: 50px; padding-top: 20px">
        <el-form
          :model="modelValue.form"
          :rules="modelValue.rules"
          ref="addEditRef"
          :label-width="110"
        >
          <el-form-item label="编码" prop="number">
            <el-input
              placeholder="请输入编码"
              v-model="modelValue.form.number"
              autocomplete="off"
              :disabled="!!modelValue.form.id"
            />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input
              placeholder="请输入名称"
              v-model="modelValue.form.name"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="状态" prop="enabled">
            <el-select
              style="width: 100%"
              v-model="modelValue.form.enabled"
              placeholder="选择状态"
            >
              <el-option
                v-for="option in statusOptions"
                :key="option.id"
                :label="option.name"
                :value="option.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请输入备注内容"
              v-model="modelValue.form.remark"
              autocomplete="off"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer v-if="!modelValue.form.disabled">
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="ensure"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import {
  addSpecificationConfig,
  updateSpecificationConfig,
} from '@/api/fms-specification-config.api'
import { statusOptions } from '@/utils/common.utils'

const props = defineProps(['modelValue'])
const addEditRef = ref<FormInstance>()

/***
 * 确认弹框
 */
const ensure = () => {
  let form = props.modelValue.form
  let formData = {
    enabled: form.enabled,
    createTime: form.createTime,
    updateTime: form.updateTime,
    createBy: form.createBy,
    updateBy: form.updateBy,
    version: form.version,
    mcNo: form.mcNo,
    number: form.number,
    name: form.name,
  }

  if (!addEditRef.value) return
  addEditRef.value.validate((valid, fields) => {
    if (valid) {
      if (form.id) {
        updateSpecificationConfig({
          ...formData,
          id: form.id,
        })
          .then((res) => {
            cancel()
            ElMessage({
              message: props.modelValue.title + '修改成功',
              type: 'success',
            })
            if (props.modelValue.refreshDataList) {
              props.modelValue.refreshDataList()
            }
          })
          .catch((err) => {
            ElMessage({
              message: err.msg || err,
              type: 'error',
            })
          })
        return
      }

      addSpecificationConfig(formData)
        .then((res) => {
          cancel()
          ElMessage({
            message: props.modelValue.title + '新增成功',
            type: 'success',
          })
          if (props.modelValue.refreshDataList) {
            props.modelValue.refreshDataList()
          }
        })
        .catch((err) => {
          ElMessage({
            message: err.msg || err,
            type: 'error',
          })
        })
    }
  })
}

/***
 * 取消弹框
 */
const cancel = () => {
  props.modelValue.visible = false
}
</script>
<style lang="scss" scoped></style>
